package filenavigation

import (
	"gitlab.local/DO-module/new-filemanage-module/src/core/fileoperate"
	fileoperate2 "gitlab.local/TerraMaster/tos-modules/fileoperate"
	"gitlab.local/golibrary/utils"
	"os"
	"sort"
)

type CollectList struct {
	fileoperate2.FoldersInfo
	IsExist bool `json:"is_exist"` //是否存在path
}

// GetCollectListFilter 获取收藏文件列表信息(过滤)
func (n *FileNav) GetCollectListFilter(order bool, rule fileoperate.OrderRule, name string, page, limit int) []CollectList {
	return n.GetCollectList(order, rule, name, page, limit)
}

// GetCollectList 获取收藏文件列表信息
func (n *FileNav) GetCollectList(order bool, rule fileoperate.OrderRule, name string, page, limit int) []CollectList {
	data := make([]CollectList, 0)

	list, err := n.navigation.collect.GetAllCollectConf(name)
	if err != nil {
		return data
	}

	confMap := make([]collectListData, 0)

	for _, v := range list {
		stat, err := os.Stat(v.Path)
		if err != nil {
			continue
		}
		confMap = append(confMap, collectListData{
			FileInfo: stat,
			path:     v.Path,
		})
	}
	if rule == "" {
		rule = fileoperate.Nature
	}

	folderData := make([]CollectList, 0)
	confMap = n.sort(confMap, rule, order)
	if page > 0 {
		startIndex := (page - 1) * limit
		endIndex := page * limit
		if endIndex > 0 {
			confMap = confMap[startIndex:endIndex]
		}
	}

	for _, v := range confMap {
		folder, err := n.fOptGetter.GetFolder(v.path)
		if err != nil {
			continue
		}
		folder.IsCollect = true
		folder.VolumePath, _ = utils.SwitchShowName(v.path)
		if folder.FType == utils.Folder {
			folderData = append(folderData, CollectList{
				FoldersInfo: *folder,
				IsExist:     true,
			})
		} else {
			data = append(data, CollectList{
				FoldersInfo: *folder,
				IsExist:     true,
			})
		}
	}

	return append(folderData, data...)
}

func (n *FileNav) sort(data []collectListData, rule fileoperate.OrderRule, order bool) []collectListData {

	sort.SliceStable(data, func(i, j int) bool {
		switch rule {
		case fileoperate.Time:
			if data[i].ModTime().After(data[j].ModTime()) {
				if order { //a>b 为降序时触发
					return true
				}
			} else {
				if !order { //a<b 为升序时触发
					return true
				}
			}
		case fileoperate.Size:
			var a, b int64
			if data[i].IsDir() {
				a, _ = n.fOptGetter.FolderSize(data[i].path, nil)
			} else {
				a = data[i].Size()
			}
			if data[j].IsDir() {
				b, _ = n.fOptGetter.FolderSize(data[j].path, nil)
			} else {
				b = data[j].Size()
			}
			if a > b {
				if order { //a>b 为降序时触发
					return true
				}
			} else {
				if !order { //a<b 为升序时触发
					return true
				}
			}
		case fileoperate.Nature:
			if data[i].Name() > data[j].Name() {
				if order { //a>b 为降序时触发
					return true
				}
			} else {
				if !order { //a<b 为升序时触发
					return true
				}
			}
		}

		return false
	})

	return data
}

func (n *FileNav) IsCollect(uname string) bool {
	return n.navigation.collect.IsCollect(uname)
}

func (n *FileNav) IsCollectList(uname, p string) bool {
	return n.navigation.collect.IsCollectList(uname, p)
}

func (n *FileNav) DelCollect(uname string, paths []string) error {
	return n.navigation.collect.DelCollect(uname, paths)
}

func (n *FileNav) AddCollectFilter(uname string, data []string) error {
	return n.navigation.collect.AddCollectFilter(uname, data)
}
